﻿using System;

namespace SpiderMonkey
{
    public sealed class JSObject : NativeObject
    {
        internal JSObject(IntPtr ptr, bool delete = true)
            : base(ptr, delete)
        {
        }

        protected override void Dispose(bool disposing)
        {
        }

        public JSVal ToValue()
        {
            return new JSVal(Native.OBJECT_TO_JSVAL(Pointer));
        }
        /// <summary>
        /// Create a new JavaScript object for use as a global object.
        /// </summary>
        /// <param name="context">The context in which to create the new global object. Requires request. (In a JS_THREADSAFE build, the caller must be in a request on this JSContext.)</param>
        /// <param name="globalClass">The class to use for the new global object.</param>
        public static JSObject NewGlobalObject(Context context, Class globalClass)
        {
            IntPtr ptr = Native.JS_NewCompartmentAndGlobalObject(context.Pointer, globalClass.ToNative(), IntPtr.Zero);
            if (ptr == IntPtr.Zero) throw new SpiderException();
            return new JSObject(ptr);
        }
        internal static JSObject Reference(IntPtr ptr)
        {
            return new JSObject(ptr, false);
        }
    }
}
